/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.com.cn / www.i-gray.cn
 *
 *	[ WARNING ]
 *		This is NOT a freeware!
 *		You MUST get author's authorization before using it!
 *	[ WARNING ]
 *
 *	Name : user-login.js / Development Code Evolve
 *	Created / Modify : 2010-3-24 / --
 */

var	weUserLogin	=	Class({
	init	:		function ( obj, parent )
	{
		this.obj	=	obj;
		this.value	=	MDK.String.trim( obj.value );
		this.span	=	$$( 'span', this.obj.parentNode )[0];

		this.parent	=	parent ? parent : false;
	},

	checkLogin	:	function ()
	{
		var	$M	=	this;
		var	username	=	MDK.String.trim( $( 'login-username' ).value );
		var	password	=	MDK.md5( MDK.String.trim( $( 'login-password' ).value ) );
		var	authcode	=	MDK.String.trim( $( 'login-authcode' ).value );

		if( !this.parent.widget.isUsername( username ) )
		{
			$( 'login-username' ).focus();
			this.span.innerHTML	=	'<u>用户名输入有误</u>';
			return	false;
		}
		else if( password == '' || password == null )
		{
			$( 'login-password' ).focus();
			this.span.innerHTML	=	'<u>密码不能为空</u>';
			return	false;
		}
		else if( authcode == '' || authcode == null || !this.parent.widget.isNumber( authcode ) )
		{
			$( 'login-authcode' ).focus();
			this.span.innerHTML	=	'<u>验证码错误</u>';
			return	false;
		}
		else
		{
			var	data	=	'username=' + username + '&password=' + password + '&authcode=' + authcode + '&hash=' + $M.parent.widget.hash( 60 );

			var	doLogin	=	function ()
			{
				if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					var	code	=	MDK.Ajax.xmlHttp.responseText.substr( 0, 6 );	//	[CH]	获取返回信息码
					var	msg		=	MDK.Ajax.xmlHttp.responseText.substr( 7 );		//	[CH]	获取返回信息
					var	sec		=	5;

					var	times	=	function ()
					{
						sec--;
						if( sec != 0 )
							var	timerId	=	window.setInterval( times, 1000 );
						else
						{
							clearInterval( timerId );
							location.reload( true );
						}
					}

					if( code == 'Err[4]' )
					{
						msg	=	'<a onclick="location.reload(true)">' + msg + '</a>';
						$M.parent.dhtml.loading( msg );
						times();
					}
					else
					{
						msg	=	'<a onclick="$(\'MDKloading\').style.display=\'none\';$(\'me-login-window\').style.display=\'block\'">' + msg + '</a>';
						$M.parent.dhtml.loading( msg );
					}
				}
				else
				{
					//	[CH]	禁止所有的编辑修改动作
					/*$( 'login-username' ).unselectable	=	'on';
					$( 'login-password' ).unselectable	=	'on';
					$( 'login-authcode' ).unselectable	=	'on';
					$( 'do-login' ).unselectable		=	'on';*/

					//	[CH]	将窗口隐藏掉
					$( 'me-login-window' ).style.display	=	'none';

					$M.parent.dhtml.loading( '正在验证登录信息，请稍等...' );
				}
			}

			MDK.Ajax.init( 'post', './u/login.php', doLogin, data );
		}
	}
})